<?php


namespace app\bock\type\bock;


use app\bock\type\NodeBackGround;
use app\bock\type\NodeBock;
use app\bock\type\NodeStack;
use app\bock\type\Panel;
use app\bock\type\Point;
use app\bock\type\stack\PanelStack;

abstract class IBock
{
    /**
     * 起始点的位置
     * @var Point
     */
    protected $startPoint;

    /**
     * @var array
     */
    protected $area;

    public function __construct()
    {
        $this->init();
    }

    /**
     * @param Point $startPoint
     */
    public function setStartPoint($startPoint)
    {
        $this->startPoint = $startPoint;
    }

    /**
     * @return Point
     */
    public function getStartPoint()
    {
        return $this->startPoint;
    }

    abstract public function init();

    /**
     * 获取所有有效节点
     */
    public function getAllEffNode()
    {
        $arrNode = [];

        foreach ($this->area as $rowKey=>$row)
        {
            foreach ($row as $colKey=>$item)
            {
                $computePoint = $this->startPoint->addPoint(new Point($rowKey, $colKey));

                $val = $this->area[$rowKey][$colKey];
                if ($val === 1)
                {
                    $arrNode[] = new NodeBock($computePoint);
                }
            }
        }

        return $arrNode;
    }

    /**
     * @inheritDoc
     */
    public function render(Panel $panel)
    {
        foreach ($this->area as $rowKey=>$row)
        {
            foreach ($row as $colKey=>$item)
            {
                $node = null;
                $computePoint = $this->startPoint->addPoint(new Point($rowKey, $colKey));
                $val = $this->area[$rowKey][$colKey];

                // 渲染为 1 的节点
                if ($val === 1)
                {
                    $node = new NodeBock($computePoint);
                    $panel->setPointNode($computePoint, $node);
                }
            }
        }
    }



    /**
     * 将图形内容转化为堆
     * @inheritDoc
     */
    public function toStack(PanelStack $panel)
    {
        foreach ($this->area as $rowKey=>$row)
        {
            foreach ($row as $colKey=>$item)
            {
                $node = null;
                $computePoint = $this->startPoint->addPoint(new Point($rowKey, $colKey));
                $val = $this->area[$rowKey][$colKey];

                if ($val === 1)
                {
                    $node = new NodeStack($computePoint);
                    $panel->addPointStack($computePoint, $node);
                }
            }
        }
    }

    /**
     * @param array $area
     */
    public function setArea($area)
    {
        // 设置的时候
        $this->area = $area;
    }

    /**
     * @return array
     */
    public function getArea()
    {
        return $this->area;
    }

    /**
     * 创建旋转过后的图形
     */
    public function rotate()
    {
        /**
         * 00  02
         * 01  12
         * 02  22
         *
         * 10  01
         * 11  11
         * 12  21
         *
         * 20  00
         * 21  10
         * 22  20
         */

        $temp = [
            [$this->area[0][2], $this->area[1][2], $this->area[2][2]],
            [$this->area[0][1], $this->area[1][1], $this->area[2][1]],
            [$this->area[0][0], $this->area[1][0], $this->area[2][0]]
        ];

        $bock = new static();
        $bock->setArea($temp);
        $bock->setStartPoint($this->startPoint);

        return $bock;
    }

}